import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path';
import checker from 'vite-plugin-checker';

import viteCompression from 'vite-plugin-compression'
import { visualizer } from 'rollup-plugin-visualizer';

import cdn from 'vite-plugin-cdn-import'
import tailwindcss from "tailwindcss"

// https://vitejs.dev/config/
function myPlugin() {
    return {
        name: 'my-plugin',
        transform(code, id) {
            // console.log('code',id,'-----',code);
            if (id.endsWith('About.tsx')) {
            //    console.log('code',id,'-----',code);
                // 对 JavaScript 文件进行处理
                return code.replace(/console\.log/g, 'console.log');
            }
        },
    };
}

export default defineConfig({
  plugins: [react(),
        myPlugin(),
        // visualizer({
        //     open: true, // 注意这里要设置为true，否则无效，如果存在本地服务端口，将在打包后自动展示
        //     gzipSize: true,
        //     brotliSize: true
        // }),
        cdn({
            modules: [
                {
                    name: 'react',
                    var: 'React',
                    path: `https://cdn.bootcdn.net/ajax/libs/react/18.3.1/umd/react.production.min.js`,
                },
                {
                    name: 'react-dom',
                    var: 'ReactDOM',
                    path: `https://cdn.bootcdn.net/ajax/libs/react-dom/18.3.1/umd/react-dom.production.min.js`,
                },
            ],
        }),
        viteCompression({
            verbose: true, // 默认即可
            disable: false, // 开启压缩(不禁用)，默认即可
            deleteOriginFile: false, // 删除源文件
            threshold: 5120, // 压缩前最小文件大小
            algorithm: 'gzip', // 压缩算法
            ext: '.gz' // 文件类型
        })
  ],
  resolve: {
    alias: {
      // dirname 目录名
      '@': path.resolve(__dirname, './src')
    }
  }, server: {
      host:'127.0.2.1',
    proxy: {
      '/api': {
        target: 'https://openapi.youdao.com/', //目标url
        changeOrigin: true, //支持跨域
            //重写路径,替换/api   rewrite: (path) => path.replace(/^\/api/, ""),
      },
       '/shop': {
           target: 'http://124.221.74.149:3000/', //目标url
          changeOrigin: true, //支持跨域
           rewrite: (path) => path.replace(/^\/shop/, ""),
      }
    },
    hmr:true,
    port: 4430
  },
  base:'./',
  build:{
      outDir:'output',
      minify:'terser',
      terserOptions:{
          compress: {
	    //  生产环境时移除console
            drop_console: true,
            drop_debugger: true,
        }
      },
      rollupOptions: {
          //静态文件按类型分包
          output: {
              chunkFileNames: 'static/js/[name]-[hash].js',
              entryFileNames: 'static/js/[name]-[hash].js',
              assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          }
      }
  },
    // 新增 css 配置
css: {
    postcss: {
        // plugins: [tailwindcss()],
    }
}
})
